2020-2021 Sunseeker Telemetry and Lighting System
comp_b_ex5_TAx.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
63 //******************************************************************************
64 
65 #include "driverlib.h"
66 
67 #define TIMER_A_PERIOD 0xFFFF
68 #define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ 12000
69 #define UCS_MCLK_FLLREF_RATIO 374
70 
71 Timer_A_initUpModeParam initUpModeParam = {0};
72 
73 void main (void)
74 {
75  //Stop WDT
76  WDT_A_hold(WDT_A_BASE);
77 
78  //Set DCO FLL reference = REFO
79  UCS_initClockSignal(
80  UCS_FLLREF,
81  UCS_REFOCLK_SELECT,
82  UCS_CLOCK_DIVIDER_1
83  );
84 
85  //Set ACLK = REFO
86  UCS_initClockSignal(
87  UCS_ACLK,
88  UCS_REFOCLK_SELECT,
89  UCS_CLOCK_DIVIDER_1
90  );
91 
92  //Set Ratio and Desired MCLK Frequency and init DCO
93  UCS_initFLLSettle(
96  );
97 
98 
99  //Initialize the Comparator B module
100  /*
101  * Base Address of Comparator B,
102  * Pin CB0 to Positive(+) Terminal,
103  * Reference Voltage to Negative(-) Terminal,
104  * Normal Power Mode,
105  * Output Filter On with minimal delay,
106  * Non-Inverted Output Polarity
107  */
108  Comp_B_initParam initParam = {0};
109  initParam.positiveTerminalInput = COMP_B_INPUT0;
110  initParam.negativeTerminalInput = COMP_B_VREF;
111  initParam.powerModeSelect = COMP_B_POWERMODE_NORMALMODE;
112  initParam.outputFilterEnableAndDelayLevel = COMP_B_FILTEROUTPUT_DLYLVL1;
113  initParam.invertedOutputPolarity = COMP_B_NORMALOUTPUTPOLARITY;
114  Comp_B_init(COMP_B_BASE, &initParam);
115 
116  //Set the reference voltage that is being supplied to the (-) terminal
117  /*
118  * Base Address of Comparator B,
119  * Reference Voltage of 2.0 V,
120  * Lower Limit of Vcc*(8/32) = (1/4)*Vcc,
121  * Upper Limit of Vcc*(24/32) = (3/4)*Vcc
122  * Static Mode Accuracy
123  */
124  Comp_B_configureReferenceVoltageParam refVoltageParam = {0};
125  refVoltageParam.supplyVoltageReferenceBase = COMP_B_VREFBASE_VCC;
126  refVoltageParam.lowerLimitSupplyVoltageFractionOf32 = 8;
127  refVoltageParam.upperLimitSupplyVoltageFractionOf32 = 24;
128  refVoltageParam.referenceAccuracy = COMP_B_ACCURACY_STATIC;
129  Comp_B_configureReferenceVoltage(COMP_B_BASE, &refVoltageParam);
130 
131  //Allow power to Comparator module
132  Comp_B_enable(COMP_B_BASE);
133 
134  //delay for the reference to settle
135  __delay_cycles(75);
136 
137  //Start timer TIMER_A0 in up mode
138  initUpModeParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
139  initUpModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
140  initUpModeParam.timerPeriod = TIMER_A_PERIOD;
141  initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
142  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
143  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
144  initUpModeParam.timerClear = TIMER_A_DO_CLEAR;
145  initUpModeParam.startTimer = true;
146  Timer_A_initUpMode(TIMER_A0_BASE, &initUpModeParam);
147 
148  //Start timer TIMER_A1 in up mode
149  initUpModeParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
150  initUpModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
151  initUpModeParam.timerPeriod = TIMER_A_PERIOD;
152  initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
153  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
154  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
155  initUpModeParam.timerClear = TIMER_A_DO_CLEAR;
156  initUpModeParam.startTimer = true;
157  Timer_A_initUpMode(TIMER_A1_BASE, &initUpModeParam);
158 
159  //Capture Falling Edge
160  Timer_A_initCaptureModeParam initCaptureMode = {0};
161  initCaptureMode.captureRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
162  initCaptureMode.captureMode = TIMER_A_CAPTUREMODE_FALLING_EDGE;
163  initCaptureMode.captureInputSelect = TIMER_A_CAPTURE_INPUTSELECT_CCIxB;
164  initCaptureMode.synchronizeCaptureSource = TIMER_A_CAPTURE_SYNCHRONOUS;
165  initCaptureMode.captureInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
166  initCaptureMode.captureOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE;
167  Timer_A_initCaptureMode(TIMER_A0_BASE, &initCaptureMode);
168 
169  //Capture Rising Edge, Enable Interrupt
170  Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,
171  TIMER_A_CAPTURECOMPARE_REGISTER_1);
172 
173  initCaptureMode.captureRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
174  initCaptureMode.captureMode = TIMER_A_CAPTUREMODE_RISING_EDGE;
175  initCaptureMode.captureInputSelect = TIMER_A_CAPTURE_INPUTSELECT_CCIxB;
176  initCaptureMode.synchronizeCaptureSource = TIMER_A_CAPTURE_SYNCHRONOUS;
177  initCaptureMode.captureInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
178  initCaptureMode.captureOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE;
179  Timer_A_initCaptureMode(TIMER_A1_BASE, &initCaptureMode);
180 
181  //Set P1.0 to output direction
182  GPIO_setAsOutputPin(
183  GPIO_PORT_P1,
184  GPIO_PIN0
185  );
186 
187  //Enter LPM0 with global interrupts enabled
188  __bis_SR_register(LPM3_bits + GIE);
189 
190  //For Debug
191  __no_operation();
192 }
193 
194 //******************************************************************************
195 //
196 //This is the TIMER1_A1_VECTOR interrupt vector service routine.
197 //
198 //******************************************************************************
199 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
200 #pragma vector=TIMER1_A1_VECTOR
201 __interrupt
202 #elif defined(__GNUC__)
203 __attribute__((interrupt(TIMER1_A1_VECTOR)))
204 #endif
205 void TIMER_A (void)
206 {
207  uint16_t temp;
208 
209  switch ( TA1IV){
210  case 2:
211 
212  //Re-Start timer TIMER_A0
213  initUpModeParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
214  initUpModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
215  initUpModeParam.timerPeriod = TIMER_A_PERIOD;
216  initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
217  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
218  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
219  initUpModeParam.timerClear = TIMER_A_DO_CLEAR;
220  initUpModeParam.startTimer = true;
221  Timer_A_initUpMode(TIMER_A0_BASE, &initUpModeParam);
222 
223  //Re-Start timer TIMER_A1
224  initUpModeParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
225  initUpModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
226  initUpModeParam.timerPeriod = TIMER_A_PERIOD;
227  initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
228  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
229  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
230  initUpModeParam.timerClear = TIMER_A_DO_CLEAR;
231  initUpModeParam.startTimer = true;
232  Timer_A_initUpMode(TIMER_A1_BASE, &initUpModeParam);
233 
234 
235  //Compare On and off time of input signal
236  temp = (Timer_A_getCaptureCompareCount(TIMER_A1_BASE,
237  TIMER_A_CAPTURECOMPARE_REGISTER_1
238  )) >> 1;
239 
240  if (Timer_A_getCaptureCompareCount(TIMER_A0_BASE,
241  TIMER_A_CAPTURECOMPARE_REGISTER_1
242  ) > temp){
243  //set P1.0
245  GPIO_PORT_P1,
246  GPIO_PIN0
247  );
248  } else {
249  //Clear P1.0 LED off
251  GPIO_PORT_P1,
252  GPIO_PIN0
253  );
254  }
255  break;
256  case 4: break; //CCR2 not used
257  case 6: break; //CCR3 not used
258  case 8: break; //CCR4 not used
259  case 10: break; //CCR5 not used
260  case 12: break; //Reserved not used
261  case 14: //Overflow
262  __no_operation();
263 
264  //If input frequency < 200Hz, trap here
265  while (1) ;
266  default: break;
267  }
268 }
long temp
#define UCS_MCLK_FLLREF_RATIO
void main(void)
Timer_A_initUpModeParam initUpModeParam
void TIMER_A(void)
#define TIMER_A_PERIOD
#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ
__no_operation()
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)